package com.google.code.timetrail.tests;

import static org.junit.Assert.*;

import java.io.File;
import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;

import com.google.code.timetrail.backend.*;
/**
 * 
 * @author Steven Miller
 *
 */
public class GameSaveTest {
	
	//Just a word of warning, this test will wipe the savegames database if 
	//you run it (to be fixed in the future?)
	
	private GameSave testSave;
	
	private File dataBaseRemover;
	
	private Control basic,sassoons,bass,captain;
	
	private StringBuilder resultTrimmer;
	
	@Before
	public void prepGameSave() {
		try{
			testSave = new GameSave();
		} catch(Exception e){
			e.printStackTrace();
		}
		basic = new Control();
		sassoons = new Control();
		bass = new Control();
		captain = new Control();
		
		sassoons.setPace(TravelSpeed.MACH);
		sassoons.setRations(ConsumptionRate.NORMAL);
		
		ArrayList<Person> members = new ArrayList<Person>();
		members.add(new Person());
		members.add(new Person());
		
		bass.setLastPlace(Place.OREGON);
		bass.setMembers(members);
		
		captain.setNextEvent(new DamagedShipEvent(captain));
	}
	//This test kind of has circular dependencies, please don't notice them
	
	@Test
	public void testSaveGame() {
		try {	
			//check some easy zero space names
			testSave.saveGame(basic, "szhzxcbvNjklnLbrer");
			testSave.saveGame(captain, "xxSkrillexSSxzis");
			testSave.saveGame(bass, "Joe");
			testSave.saveGame(sassoons, "abcedgBseSosg");
		
		    //check some easy mult-space names
			testSave.saveGame(basic, "The Quick Brown Fox Jumps Over the Lazy Dog");
			testSave.saveGame(bass, "Bomber Man");
			testSave.saveGame(sassoons, "Caved In Johnson");
			testSave.saveGame(captain, "THEBEST TEhBEST BTEHSST THEBESET");
			
			//test some unicode zero space names
			testSave.saveGame(basic, "\u00F6\u1111\u2603\u303F");
			testSave.saveGame(captain, "\u3020\u2196\u2195\u7832\u0012\u65f2");
			testSave.saveGame(bass, "\u0089\u2209");
			testSave.saveGame(sassoons, "\u7463");
			
			//test some unicode names with spaces
			testSave.saveGame(basic, "\u9999 \u4752 \u3432");
			testSave.saveGame(bass, "\u3245\u6234\u0987 \u2345\u7323\uA434");
			testSave.saveGame(sassoons, "\u3020\u2196\u2195\u2345\u7323 \u3245\u6234 \u0987");
			testSave.saveGame(captain, "\u2196\u2195\u7832 \u6234\u0987");
			
			//test for newlines and carriage returns
			testSave.saveGame(basic, "\n\t\b\f\r");
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Test
	public void testListOfEntries() {
	String[] testStrings = {"szhzxcbvNjklnLbrer","xxSkrillexSSxzis","Joe","abcedgBseSosg",
				"The Quick Brown Fox Jumps Over the Lazy Dog","Bomber Man", "Caved In Johnson",
				"THEBEST TEhBEST BTEHSST THEBESET","\u00F6\u1111\u2603\u303F","\u3020\u2196\u2195\u7832\u0012\u65f2",
				"\u0089\u2209","\u7463","\u9999 \u4752 \u3432","\u3245\u6234\u0987 \u2345\u7323\uA434",
				"\u3020\u2196\u2195\u2345\u7323 \u3245\u6234 \u0987","\u2196\u2195\u7832 \u6234\u0987"};
		try {
			for(int i = 0; i < testSave.listOfEntries().length; i++){
				resultTrimmer = new StringBuilder(testSave.listOfEntries()[i]);
				resultTrimmer.setLength(testStrings[i].length());
				resultTrimmer.trimToSize();
				assertEquals(testStrings[i],resultTrimmer.toString());
			}
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Test
	public void testLoadGame() {
		try {
			String[] loadStrings;
			String name = "";
			String date = "";
			Control[] controllers = {basic,captain,bass,sassoons,basic,bass,sassoons,
					                 captain,basic,captain,bass,sassoons,basic,bass,sassoons,captain};
			for(int i = 0; i < testSave.listOfEntries().length; i++){
				loadStrings = testSave.listOfEntries()[i].split(" ");
				for(int j = 0; j <loadStrings.length; j++){
					if(j < loadStrings.length - 2) {
						name += loadStrings[j];
						if(j < loadStrings.length - 3) {
							name += " ";
						}
					}
					else{
						date += loadStrings[j];
						if(j < loadStrings.length  - 1){
							
						}
					}
				}
				assertEquals(controllers[i],testSave.loadGame(name, date));
			}
		} catch(Exception e) {
			
		}
	}
	
	
	@Test
	public void cleanUpDatabase() {
		
		dataBaseRemover = new File(testSave.getDatabaseLocation());
		dataBaseRemover.delete();
	}
}
